package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVoService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 当项目启动完成时,将mysql中数据加载到redis中
 * redis 数据预热
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {


    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisVoService strategyStatisVoService;

    /**
     * spring容器启动,并初始化完成时,必须执行的操作
     *
     * mysql --- vo --- redis
     * @param event
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("-------------------vo数据初始化开始----------------");
        List<StrategyDetail> details = strategyDetailService.list();
        for (StrategyDetail detail : details) {

            //如果已存在的vo,不进行初始化操作,避免数据的覆盖
            if(strategyStatisVoService.isStrategyStatisVo(detail.getId())){
                continue;
            }

            StrategyStatisVo strategyStatisVo = new StrategyStatisVo();
            BeanUtils.copyProperties(detail,strategyStatisVo);
            strategyStatisVo.setStrategyId(detail.getId());
            strategyStatisVo.setDestId(detail.getDest().getId());
            strategyStatisVo.setDestName(detail.getDest().getName());

            strategyStatisVoService.setStrategyStatisVo(strategyStatisVo);

        }

        System.out.println("-------------------vo数据初始化完成----------------");

        System.out.println("-------------------攻略排行数据初始化开始-----------");

        /**
         * 热门攻略排行初始化
         */
        for (StrategyDetail detail : details) {

            String hotStrategyZsetKey = RedisKeys.HOT_STRATEGY_ZSET.getPrefix();

            String voKey = RedisKeys.STRATEGY_STATIS_VO.jion(detail.getId().toString());

            if(strategyStatisVoService.isVokeyExistsZSet(hotStrategyZsetKey,voKey)){
                continue;
            }

            int score = detail.getViewnum()+detail.getReplynum();
            strategyStatisVoService.addHotStrategyScore(detail.getId(),score);
        }

        /**
         * 国内外攻略排行初始化
         */
        for (StrategyDetail detail : details) {

            String inlandOrAbroadZsetKey = null;
            if(detail.isIsabroad()){
                //国外
                inlandOrAbroadZsetKey = RedisKeys.ABROAD_STRATEGY_ZSET.getPrefix();
            }else{
                inlandOrAbroadZsetKey = RedisKeys.INLAND_STRATEGY_ZSET.getPrefix();
            }
            String voKey = RedisKeys.STRATEGY_STATIS_VO.jion(detail.getId().toString());
            if(strategyStatisVoService.isVokeyExistsZSet(inlandOrAbroadZsetKey,voKey)){
                continue;
            }

            int score = detail.getThumbsupnum() + detail.getFavornum();
            strategyStatisVoService.addInlandAndAbroadStrategyScore(detail.getId(),score);
        }

        System.out.println("-------------------攻略排行数据初始化完成-----------");
    }
}
